package com.fare.taluo.application.plane;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.vo.core.AnalyseBaseVo;
import com.fare.taluo.vo.core.PlaneInfo;
/**
 * (保留)
 * @author Administrator
 *
 */
@Transactional
public abstract class AnalyseService {
	
	public static List<String> calculateDays(String beginDateStr,String endDateStr){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StringUtils.isEmpty(beginDateStr)&&!StringUtils.isEmpty(endDateStr)) {
			try{
				Date beginDate = formatter.parse(beginDateStr);
				Date endDate = formatter.parse(endDateStr);
				Calendar c = Calendar.getInstance();
				c.setTime(endDate);
				c.set(Calendar.DATE, c.get(Calendar.DATE)+1);
				endDate = c.getTime();
				List<String> dateList = new ArrayList<String>();
				for(long time = beginDate.getTime(); time <= endDate.getTime();){
					dateList.add(formatter.format(new Date(time)));
					time += 1000 * 60 * 60 * 24;
				}
				return dateList;
			}catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	
	public static String assembleJSONByDate(Map<String, Map<String, String>> dataMap,Collection<String> airCompanyList,List<String> dateList,String tableHead,int total, long page, long rows){
		StringBuffer resultBuffer = new StringBuffer();
		resultBuffer.append("{'Total':'"+total+"',"+"'Rows':[");
		for(String date: dateList){
			resultBuffer.append("{'"+tableHead+"':'"+date+"'");
//			for(int i = 0; i < airCompanyList.size(); i++){
			for(String key: airCompanyList){
				resultBuffer.append(",'"+key+"':'"+dataMap.get(date).get(key)+"'");
			}
			resultBuffer.append("},");
		}
		if(resultBuffer.toString().endsWith(","))
			resultBuffer.deleteCharAt(resultBuffer.lastIndexOf(","));
		resultBuffer.append("]}");
		return resultBuffer.toString();
	}

	/**
	 * 数据分析并返回结果集
	 * @param csairPriceMap
	 * @param qunarPriceMap
	 * @return
	 */
	public abstract List<String> analyse(Map<String, PlaneInfo> csairPriceMap,
			Map<String, PlaneInfo> qunarPriceMap);
	
	
	public static String getColumnsInJSON(String tableHead,String tableHeadStr,Collection<String> columns){
		StringBuffer sb = new StringBuffer();
		sb.append("[{display:'"+tableHeadStr+"',name:'"+tableHead+"',width:120,height:100},");
		for(String column:columns){
			sb.append("{display:'"+column+"',name:'"+column+"',width:120,height:50},");
		}
		if(sb.toString().endsWith(","))
			sb.deleteCharAt(sb.lastIndexOf(","));
		sb.append("]");
		return sb.toString();
	}
	
	public static void handleListParam(List<String> list){
		if(list==null||list.size()==0) return;
		String paramStr = list.get(0);
		paramStr = paramStr.substring(1, paramStr.length()-1);
		String[] params = paramStr.split(",");
		for(int i = 0; i < params.length; i++){
			list.set(i, params[i].trim());
		}
	}
	
	/**
	 * 根据输入的相对日期偏移数计算出日期数组
	 * @param beginDate与endDate皆为整形的str
	 * @return
	 * @throws ParseException 
	 */
	public static List<String> calculateDaysByNow(String beginDateStr,String beginDate,String endDate) throws NumberFormatException, ParseException{
		int beginDateInt = Integer.parseInt(beginDate);
		int endDateInt = Integer.parseInt(endDate);
		String start = DateUtils.tranferDate(beginDateStr,beginDateInt);
		String end = DateUtils.tranferDate(beginDateStr,endDateInt);
		return calculateDays(start, end);
	}
	
	
	/**
	 * 
	 * 传入vo数组取得JSON
	 * @param resultList
	 * @param total
	 * @return
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public String toJSON(List<? extends AnalyseBaseVo> vos,int total) throws JsonGenerationException, JsonMappingException, IOException{
		String jsonStr = "{\"Total\":"+total+","+
						 "\"Rows\":";
		int i = 0;
		String json = JsonUtil.toJson(vos);
		jsonStr += json;
		jsonStr += "}";
		return jsonStr;
	}
	
	public Map<String,List<String>> assembleFusionChartXml(Map<String,List<String>> chartMap,String dateStr,String key,Map<String, PlaneInfo> infoMap) {
		String price = "";
		for(String infoKey :infoMap.keySet()){
			if(infoMap.get(infoKey) != null)
			price = infoMap.get(infoKey).getLowprPrice();
		}
		if(chartMap == null){
			chartMap = new HashMap<String, List<String>>();
		}
		if(!chartMap.containsKey(key)){
			chartMap.put(key, new ArrayList<String>());
		}
		chartMap.get(key).add(price);
		return chartMap;
	}

}
